const net = require('net')
const MyTransformCode = require('./MyTransformCode') 

const trans = new MyTransformCode()
let packContent = null

const client = net.createConnection({
  port: 1234,
  host: 'localhost',
})

client.write(trans.encode('测试一下~'))
client.write(trans.encode('测试两下~'))
client.write(trans.encode('测试三下~'))
client.write(trans.encode('测试四下~'))
client.write(trans.encode('测试五下~'))
client.write(trans.encode('~'))

client.on('data', (chunk) => {
  if(packContent) {
    chunk = Buffer.concat([packContent, chunk])
  }
  let packlen = 0
  while(packlen = trans.getPackageLen(chunk)) {
    // 包内有内容
    const currentChunk = chunk.slice(0, packlen)
    chunk = chunk.slice(packlen)
    const data = trans.decode(currentChunk)
    console.log(data.body)
  }
  packContent = packContent ? Buffer.concat([packContent, chunk]) : chunk
})

